﻿using System.IO;
using PsqlDotnet;
using PsqlDotnet.Enums;
using PsqlDotnet.Model.Schema;

namespace CLITest
{
    class Program
    {
        static void Main(string[] args)
        {
            PsqlShell shell = new PsqlShell(new FileInfo(@"C:\Program Files\PostgreSQL\9.0\bin\psql.exe"), "postgres", "root");
            //PsqlShell shell = new PsqlShell(new FileInfo(@"C:\Program Files\PostgreSQL\8.4\bin\psql.exe"), "postgres", "root");

            //var dbList = shell.GetAllDatabases();
            //var tableList = shell.GetTableNames("media");
            //var t = shell.GetTableSchema("media", "listens");


            ITable table = new Table("test_table");

            table.AddColumn(new Column("id", new ColumnDataInfo()
            {
                DataType = DbDataTypeEnum.Serial,
                IsNullable = false
            }));

            table.AddColumn(new Column("name", new ColumnDataInfo()
            {
                DataType = DbDataTypeEnum.CharacterVarying,
                FieldSize = 256,
                IsNullable = false
            }));

            table.AddColumn(new Column("description", new ColumnDataInfo()
            {
                DataType = DbDataTypeEnum.CharacterVarying,
                IsNullable = true
            }));

            table.AddColumn(new Column("album_id", new ColumnDataInfo()
            {
                DataType = DbDataTypeEnum.Integer,
                IsNullable = false
            }));

            table.AddConstraint(new PrimaryKey("PK_test", new string[] { "id" }, "test_table"));
            table.AddConstraint(new ForeignKey(new ForeignKeySettings()
            {
                Name = "fk_test",
                ReferencedColumns = new string[] { "id"},
                ReferencedTable = "albums",
                SourceColumns = new string[] { "album_id"},
                SourceTable = "test_table",
            }));
            table.AddConstraint(new UniqueConstraint("uq_test_name", "name", "test_table"));


            shell.CreateTable("media", table);

            //const string dbName = "media";
            //const string userName = "tillias";

            ////if (shell.DatabaseExists(dbName))
            ////    shell.RemoveDatabase(dbName);

            //if (!shell.UserExists(userName))
            //    shell.CreateUser("tillias", "qwerty");

            //if (!shell.DatabaseExists(dbName))
            //{
            //    shell.CreateDatabase(dbName, userName);
            //}

            //shell.GrantPublicPrivileges("tags", new DbPrivileges("media", TablePrivilegeFlags.All));

            ////shell.GrantPrivilegesToUser(;

            ////shell.AddUserToGroup("first", "john");
            ////shell.RemoveUserFromGroup("first", "john");

            //var list2 = shell.GetAllGroups();

            //var list2 = shell.GetAllUsers();
        }
    }
}
